package com.wgl.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgl.model.enums.RefundStatusEnum;
import com.wgl.model.order.PaymentInfo;
import com.wgl.model.order.RefundInfo;
import com.wgl.order.mapper.RefundMapper;
import com.wgl.order.service.RefundService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName RefundServiceImpl.java
 * @Description TODO
 * @createTime 2022年02月15日 22:51:00
 */
@Service
public class RefundServiceImpl extends ServiceImpl<RefundMapper, RefundInfo> implements RefundService {
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        QueryWrapper<RefundInfo> wrapper = new QueryWrapper();
        wrapper.eq("order_id",paymentInfo.getOrderId());
        wrapper.eq("payment_type",paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(wrapper);
        if (refundInfo != null){
            return refundInfo;
        }
        //有相同的
        refundInfo = new RefundInfo();

        refundInfo.setCreateTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
